﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VASJ.Model;
using System.Web.Security;
using VASJ.Service.Library;
using System.Web.Profile;
using System.Web.Configuration;

namespace VASJ.Service
{
    public partial class AcpInetService
    {
        #region "AHSProfileProvider"
        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// DeleteInactiveProfiles
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="userInactiveSinceDate"></param>
        /// <returns></returns>
        public int DeleteInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            return ProfileManager.Provider.DeleteInactiveProfiles(authenticationOption, userInactiveSinceDate);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// DeleteProfiles
        /// </summary>
        /// <param name="usernames"></param>
        /// <returns></returns>
        public int DeleteProfiles(string[] usernames)
        {
            return ProfileManager.Provider.DeleteProfiles(usernames);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// DeleteProfilesCollection
        /// </summary>
        /// <param name="profiles"></param>
        /// <returns></returns>
        public int DeleteProfilesCollection(ProfileInfoCollection profiles)
        {
            return ProfileManager.Provider.DeleteProfiles(profiles);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// FindInactiveProfilesByUserName
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="usernameToMatch"></param>
        /// <param name="userInactiveSinceDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public ProfileInfoCollection FindInactiveProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
        {
            return ProfileManager.Provider.FindInactiveProfilesByUserName(authenticationOption, usernameToMatch, userInactiveSinceDate, pageIndex, pageSize, out totalRecords);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// FindProfilesByUserName
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="usernameToMatch"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public ProfileInfoCollection FindProfilesByUserName(ProfileAuthenticationOption authenticationOption, string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
        {
            return ProfileManager.Provider.FindProfilesByUserName(authenticationOption, usernameToMatch, pageIndex, pageSize, out totalRecords);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetAllInactiveProfiles
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="userInactiveSinceDate"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public ProfileInfoCollection GetAllInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate, int pageIndex, int pageSize, out int totalRecords)
        {
            return ProfileManager.Provider.GetAllInactiveProfiles(authenticationOption, userInactiveSinceDate, pageIndex, pageSize, out totalRecords);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetAllProfiles
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="totalRecords"></param>
        /// <returns></returns>
        public ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
        {
            return ProfileManager.Provider.GetAllProfiles(authenticationOption, pageIndex, pageSize, out totalRecords);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetNumberOfInactiveProfiles
        /// </summary>
        /// <param name="authenticationOption"></param>
        /// <param name="userInactiveSinceDate"></param>
        /// <returns></returns>
        public int GetNumberOfInactiveProfiles(ProfileAuthenticationOption authenticationOption, DateTime userInactiveSinceDate)
        {
            return ProfileManager.Provider.GetNumberOfInactiveProfiles(authenticationOption, userInactiveSinceDate);
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// CreateProfile
        /// </summary>
        /// <param name="username"></param>
        /// <param name="info"></param>
        public void CreateProfile(string username, Dictionary<string, object> info)
        {
            ProfileBase pf = ProfileBase.Create(username);

            foreach (KeyValuePair<string, object> it in info)
            {
                pf.SetPropertyValue(it.Key, it.Value);
            }

            pf.Save();
        }

        //[PrincipalPermission(SecurityAction.Demand, Role = "ahs_service")]
        /// <summary>
        /// GetProfile
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public Dictionary<string, object> GetProfile(string username)
        {
            Dictionary<string, object> retVal = new Dictionary<string, object>();

            ProfileBase pf = ProfileBase.Create(username);

            WebConfigurationManager.GetSection("system.web/profile/properties");

            ProfileSection profileSection = (ProfileSection)WebConfigurationManager.GetSection("system.web/profile");

            string[] properties = profileSection.PropertySettings.AllKeys;

            for (int i = 0; i < properties.Length; i++)
            {
                retVal.Add(properties[i], pf.GetPropertyValue(properties[i]));
            }

            return retVal;
        }
        #endregion
    }
}